Skip to content

[protobuf-schema] Add flag to handle complex type#20915

Merged
wing328 merged 6 commits intoOpenAPITools:masterfrom
lucy66hw:wrap_complex_type
Mar 20, 2025
Merged

[protobuf-schema] Add flag to handle complex type#20915
wing328 merged 6 commits intoOpenAPITools:masterfrom
lucy66hw:wrap_complex_type

Conversation

@lucy66hw
Copy link
Copy Markdown
Contributor

ref #20472
Currently, the Protobuf generator in OpenAPI Generator does not correctly handle complex types, such as:

Maps inside oneof
Arrays inside oneof
Arrays inside maps
Maps inside arrays
When compiling the generated Protobuf schema, these cases result in compilation failures due to structural incompatibilities.

This PR introduces a flag wrapComplexType to have preprocessing logic that modifies the OpenAPI Schema before code generation. It achieves this by adding extra schema definitions to ensure that complex types are correctly transformed into compatible Protobuf structures.

Example:

Tag:
      properties:
        id:
          type: integer
          format: int64
        name:
          oneOf:
            - type: string
            - type: array
              items:
                  type: string
            - type: object
              title: map
              additionalProperties:
                type: string

Before:

message TagName {
 
     oneof tag_name {
         string string = 1;
         repeated string string_array = 2;
         map<string, string> map = 3;
     }
 }

with introduced flag wrapComplexType:

message TagName {
     oneof tag_name {
         string string = 1;
         StringArray string_array = 2;
         StringMap map = 3;
     }
 }

message StringArray {
   repeated string string_array = 1;
 }

message StringMap {
   map<string, string> string_map = 1;
 }

PR checklist

  • Read the contribution guidelines.
  • Pull Request title clearly describes the work in the pull request and Pull Request description provides details about how to validate the work. Missing information here may result in delayed response from the community.
  • Run the following to build the project and update samples:
    ./mvnw clean package || exit
    ./bin/generate-samples.sh ./bin/configs/*.yaml || exit
    ./bin/utils/export_docs_generators.sh || exit
    
    (For Windows users, please run the script in Git BASH)
    Commit all changed files.
    This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
    These must match the expectations made by your contribution.
    You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example ./bin/generate-samples.sh bin/configs/java*.
    IMPORTANT: Do NOT purge/delete any folders/files (e.g. tests) when regenerating the samples as manually written tests may be removed.
  • File the PR against the correct branch: master (upcoming 7.x.0 minor release - breaking changes with fallbacks), 8.0.x (breaking changes without fallbacks)
  • If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.

Comment thread bin/configs/protobuf-schema-config.yaml Outdated
addJsonNameAnnotation: true
numberedFieldNumberList: true
startEnumsWithUnspecified: true
wrapComplexType: true No newline at end of file
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of updating this config file, what about creating a new one instead, e.g. protobuf-schema-config-wrapComplexType.yaml ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good point. created new config file for config with wrapComplexType.

}
}

private void wrapComposedChildren(List<Schema> children, Set<Schema> visitedSchema) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor suggestion: add a doc string explaining what this function does

return refSchema;
}

public String getType(Schema schema) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor suggestion: add a doc string explaining what this function does

@wing328
Copy link
Copy Markdown
Member

wing328 commented Mar 19, 2025

shall we enabled the option wrapComplexType by default as from what I understand the output won't compile without this option enabled for complex type?

(we need to explicitly disable the option in bin/configs/protobuf-schema-config.yaml if wrapComplexType is default to true)

@lucy66hw
Copy link
Copy Markdown
Contributor Author

shall we enabled the option wrapComplexType by default as from what I understand the output won't compile without this option enabled for complex type?

(we need to explicitly disable the option in bin/configs/protobuf-schema-config.yaml if wrapComplexType is default to true)

make sense. Changed wrapComplexType to true by default. Please reviewe.

@wing328 wing328 merged commit 1996d7e into OpenAPITools:master Mar 20, 2025
18 checks passed
@wing328
Copy link
Copy Markdown
Member

wing328 commented Mar 20, 2025

just merged. thanks for the contribution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants